<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.springframework.org/schema/mvc">
<head>
    <meta charset="UTF-8">
    <title>ECharts 生成报表 并保存图片</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script th:src="@{/js/echarts.js}" type="text/javascript"></script>
    <script th:href="@{/js/jquery-3.3.1.min.js}" type="text/javascript"></script>

</head>

<body>
    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'ECharts 入门示例'
            },
            tooltip: {
                show: true
            },
            legend: {
                data: ['销量']
            },
            xAxis: {
                data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子", "T恤", "卫衣", "衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子", "T恤", "卫衣"]
            },
            yAxis: {},
            series: [{
                name: '销量',
                type: 'bar',
                data: [5, 20, 36, 10, 10, 20, 50, 100, 5, 20, 36, 10, 10, 20, 50, 100]
            }]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);

        // setTimeout(exportImage, 2000);
        function exportImage(){
            // 获取生成的图表base64编码
            var picInfo  = myChart.getDataURL();
            var data = "baseimg="+encodeURIComponent(picInfo);
            var xmlhttp;
            if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else { // code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.open("POST","/downloadDocTwo",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    alert("保存成功");
                }
            }
            xmlhttp.send(data);
        };
    </script>

<script>

</script>
</body>

</html>